package reflect;

import reflect.annotations.AutoRunMethod;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URISyntaxException;

/**
 * 调用那些与当前类Test9在同一个保重被注解@AutoRunClass标注的类里所有被注解@AutoRunMethod
 * 标注的方法
 */
public class Test9 {
    public static void main(String[] args) throws URISyntaxException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException {
        Class c = Test9.class;
        String pake = c.getPackage().getName();

        File dir = new File(c.getResource(".").toURI());
        File[] files = dir.listFiles(f->f.getName().endsWith(".class"));
        for (File f : files){
            String fName = f.getName().replace(".class","");
            Class cls = Class.forName(pake+"."+fName);
            Object o = cls.newInstance();

            Method[] methods = cls.getMethods();
            for(Method m : methods){
                if(m.isAnnotationPresent(AutoRunMethod.class)){
                    Method method = cls.getDeclaredMethod(m.getName());
                    method.invoke(o);
                }
            }

        }
    }
}
